package com.coin.exchanges.view.user

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.coin.exchanges.R
import com.coin.exchanges.net.RetrofitFactory
import com.coin.exchanges.presenter.TestPresenter
import com.coin.exchanges.utils.SpUtils
import com.coin.exchanges.view.user.bean.BindCardBean
import com.coin.exchanges.view.user.bean.UserLoginBean
import com.coin.libbase.view.activity.JToolbarActivity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_bind_card.*
import java.util.HashMap
//绑定银行卡页面
class BindCardActivity : JToolbarActivity<TestPresenter>() {
    override fun getLayout(): Int {
        return R.layout.activity_bind_card
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun initView() {
        setToolbarBackground(R.color.white)
        title = "绑定银行卡"
    }

    override fun initData() {
        mBindCardTv.setOnClickListener {
            if (checkBindCard(mUserNameEt.text.toString().trim(), mPersonCodeEt.text.toString().trim(),
                            mPersonBandEt.text.toString().trim(), mPersonBandCodeEt.text.toString().trim())) {
                val userId = SpUtils.getBookBean(this@BindCardActivity).data[0].UserId
                PostNetRegisterCheckAll(userId,mUserNameEt.text.toString().trim(), mPersonCodeEt.text.toString().trim(),
                        mPersonBandEt.text.toString().trim(), mPersonBandCodeEt.text.toString().trim())
            }
        }
        iv_clear_api.setOnClickListener {
            mUserNameEt.setText("")
        }
        iv_secret_api.setOnClickListener {
            mPersonCodeEt.setText("")
        }
        mPersonBandEt.setOnClickListener {
            mPersonBandEt.setText("")
        }
        mPersonBandCodeIv.setOnClickListener {
            mPersonBandCodeEt.setText("")

        }
    }
    private fun PostNetRegisterCheckAll(user_id:String,user_name: String, user_code: String, bank_name: String, bank_code: String){
        val map = HashMap<String, String>()
        map["user_id"] = user_id
        map["user_name"] = user_name
        map["user_code"] = user_code
        map["bank_name"] = bank_name
        map["bank_code"] = bank_code
        RetrofitFactory
                .getOkExApiService()
                .bindCard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BindCardBean>{
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BindCardBean) {
                        Toast.makeText(this@BindCardActivity, t.returnval, Toast.LENGTH_LONG).show()
                            finish()
                    }

                    override fun onError(e: Throwable) {
                        Log.e("jignan",e.message)
                    }

                })
    }
    fun checkBindCard(user_name: String, user_code: String, bank_name: String, bank_code: String): Boolean {
        if (TextUtils.isEmpty(user_name) || TextUtils.isEmpty(user_code) || TextUtils.isEmpty(bank_name) || TextUtils.isEmpty(bank_code)) {
            Toast.makeText(this, "请确认填写信息", Toast.LENGTH_LONG).show()
            return false
        }
        return true
    }


}
